#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : interface.vissim_setup.py
# @Note    : "联系反馈"窗口界面
import os

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QFileDialog
from PyQt5.uic import loadUiType

ui, _ = loadUiType("./resource/ui/vissim.ui")


# 联系与反馈信息界面
class vissim_setupDialog(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.setWindowIcon(QIcon("../resource/icon/about.png"))
        self.setWindowTitle("联系反馈")
        self.setFixedSize(self.width(), self.height())
        self.pushButton_program_path.clicked.connect(self.load_program_path)
        self.pushButton_net_path.clicked.connect(self.load_net_path)
        self.pushButton_flow_path.clicked.connect(self.load_flow_path)
        self.pushButton_save_path.clicked.connect(self.load_save_path)
        self.program_path = r"E:\Program Files (x86)\PTV_Vision\VISSIM430\Exe\vissim.exe"
        self.net_path = os.getcwd().replace("\\", "/") + "/resource/vissim/net/net.inp"
        self.flow_path = os.getcwd().replace("\\", "/") + "/resource/data/vehicle_inputs.txt"
        self.save_path = os.getcwd().replace("\\", "/") + "/resource/save/"
        # 初始化仿真设置
        self.lineEdit_sim_path.setText(self.program_path)
        self.lineEdit_net_path.setText(self.net_path)
        self.lineEdit_flow_path.setText(self.flow_path)
        self.lineEdit_save_path.setText(self.save_path)

    # 加载仿真软件路径
    def load_program_path(self):
        reply = QFileDialog.getOpenFileName(self, "加载vissim启动文件", self.get_desktop_path(), "(*.exe *.lnk)")
        try:
            if reply[0]:
                self.program_path = reply[0]
                self.lineEdit_sim_path.setText(self.program_path)
                QMessageBox.information(self, "提示", "vissim启动文件路径加载成功")
            else:
                QMessageBox.information(self, "提示", "vissim启动文件路径加载失败")
        except (IndexError, Exception):
            QMessageBox.information(self, "提示", "vissim启动文件路径加载失败")

    # 加载路网文件路径
    def load_net_path(self):
        reply = QFileDialog.getOpenFileName(self, "道路仿真路网文件", "./", "(*.inp)")
        try:
            if reply[0]:
                self.net_path = reply[0]
                self.lineEdit_net_path.setText(self.net_path)
                QMessageBox.information(self, "提示", "道路仿真路网文件路径加载成功")
            else:
                QMessageBox.information(self, "提示", "道路仿真路网文件路径加载失败")
        except (IndexError, Exception):
            QMessageBox.information(self, "提示", "道路仿真路网文件路径加载失败")

    # 加载交通流量文件路径
    def load_flow_path(self):
        reply = QFileDialog.getOpenFileName(self, "交通流量文件", "./", "(*.txt *.xlsx *.xls)")
        try:
            if reply[0]:
                self.flow_path = reply[0]
                self.lineEdit_flow_path.setText(self.flow_path)
                QMessageBox.information(self, "提示", "交通流量文件路径加载成功")
            else:
                QMessageBox.information(self, "提示", "交通流量文件路径加载失败")
        except (IndexError, Exception):
            QMessageBox.information(self, "提示", "交通流量文件路径加载失败")

    # 加载评价保存路径
    def load_save_path(self):
        reply = QFileDialog.getExistingDirectory(self, "仿真结果保存路径", "./")
        try:
            if reply[0]:
                self.save_path = reply
                self.lineEdit_save_path.setText(self.save_path)
                QMessageBox.information(self, "提示", "仿真结果保存路径加载成功")
            else:
                QMessageBox.information(self, "提示", "仿真结果保存路径加载失败")
        except (IndexError, Exception):
            QMessageBox.information(self, "提示", "仿真结果保存路径加载失败")

    # 加载仿真环境参数设置
    def load_environment_setting(self):
        period = int(self.lineEdit_Sim_Period.text())
        speed = int(self.lineEdit_Sim_speed.text())
        resolution = int(self.lineEdit_Sim_resolution.text())
        control_freq = int(self.lineEdit_Control_Freq.text())
        seed = int(self.lineEdit_Random_Seed.text())
        if self.radioButton_hidden_flow.isChecked():
            visualization = False
        else:
            visualization = True
        if self.radioButton_3D_mode.isChecked():
            mode = True
        else:
            mode = False
        self.simulation = [period, speed, resolution, control_freq, seed, visualization, mode]

    # 加载车流量
    def load_traffic_flow(self, elapsed_time):
        while True:
            if elapsed_time < self.vehicle[self.num][1]:
                break
            else:
                self.num += 1
        return self.vehicle[self.num][2:]

    # 加载车流量配置文件
    def load_traffic_flow_file(self):
        with open(self.flow_path, "r") as f:
            lines = f.readlines()
        self.vehicle = []
        self.num = 0
        for i in range(1, len(lines)):
            self.vehicle.append(list(map(int, lines[i].split())))


